﻿//Our To Do Constructor
var User = function (task, chirps) {
    this.task= task,
    this.chirps= chirps
}
//Central Data Container/Array
var todoArray = [];
//This variable will keep track of current item
var currId;

//Fetch data from Firebase 
var fetch = function (callback) {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "https://cobrachirper.firebaseio.com/chirps/.json", true);
    xhr.onload = function () {
        if (this.status >= 200 && this.status < 400) {
            console.log(this.response)
            callback(JSON.parse(this.response))
        }
    }
    xhr.send();
}
fetch(showresults);

//Callback for fetch
function showresults(data) {
    var indexCounter = 0;
    var htmlstr = "<table class='table table-bordered table-condensed table-striped'>";
    for (var i in data) {
        console.log(data[i].User, data[i].chirp);
        todoArray.push({
            id: i,
            User: data[i].User,
            chirp: data[i].chirp
        })
    
        htmlstr += "<tr><td>";
        htmlstr += "<td>" + data[i].User + "</td>" + "<td>" + data[i].chirp + "</td>";
        htmlstr += "<td><button onclick='dodeletetodo(" + indexCounter + ")' class='btn btn-xs btn-danger'><span class='glyphicon glyphicon-scissors'></span></button></td>";
       // htmlstr += "<button onclick='doupdatetodo(" + indexCounter + ")' class='btn btn-xs btn-warning'><span class='glyphicon glyphicon-erase'></span></button>";
        htmlstr += "</td></tr>";
        indexCounter++;
    }
    htmlstr += "</table>"
    $("#ToDoList").html(htmlstr);
}



function doaddtodo() {
    console.log($("#todotext").val())
    console.log($("#todocompleted").val())
    var newtodo = {
        User: $("#todotext").val(),
        chirp: $("#todocompleted").val()
    }
    sendToAjax(newtodo)
}
//Send to Ajax
function sendToAjax(objToPost) {
    var request = new XMLHttpRequest();
    request.open('POST', 'https://cobrachirper.firebaseio.com/chirps/.json', true);
    request.onload = function () {
        if (this.status >= 200 && this.status < 400) {
            fetch(showresults)
        }
        else {
            //Do Something On Error
        }
    };
    request.send(JSON.stringify(objToPost));
}
function dodeletetodo(index) {
    console.log("Index to delete:", index);
    console.log(todoArray[index])
    var xhr = new XMLHttpRequest();
    xhr.open("DELETE", "https://cobrachirper.firebaseio.com/chirps/" + todoArray[index].id + "/.json", true)
    xhr.send();
    xhr.onload = function () {
        if (this.status >= 200 && this.status < 400) {
            fetch(showresults)
        }
        else {
            //Do Something On Error
        }
    };
}



//
$('#savetodo').hide();
function doupdatetodo(index) {
    console.log(todoArray[index])
    $('#todotext').val(todoArray[index].User);
    $('#todocompleted').val(todoArray[index].chirp);
    currId = todoArray[index].id;
    // toggle button visibility
    $('#addtodo').hide();
    $('#savetodo').show();
}
// save changes to FB
function dosavetodo() {
    var xhr = new XMLHttpRequest();
    xhr.open("PUT", "https://cobrachirper.firebaseio.com/Chirps/" + currId + "/.json", true)
    var newtodo = {
        todo: $("#todotext").val(),
        chirp: $("#todocompleted").val()
    }
    xhr.send(JSON.stringify(newtodo));
    xhr.onload = function () {
        if (this.status >= 200 && this.status < 400) {
            fetch(showresults)
        }
        else {
            //Do Something On Error
        }
    };
    // toggle button visibility
    $('#addtodo').show();
    $('#savetodo').hide();
    $('#todotext').val("");
    $('#todocompleted').val("");
}


//To make an update button:

//onclick
//grab the id
//fetch the correct object out of the data container
//populate the view with the fetched data
//wait for the user to click save changes
//after changes are saved, need to update that element in firebase
//after updated, reload all of the data